package com.xsp.mybatisxmlsqlparser.node;

import cn.hutool.core.util.EnumUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElementCounters {
    private final Map<String, Integer> counters;

    public ElementCounters() {
        this.counters = new HashMap<>();
        //遍历  ParseNodeTypeEnum
        List<String> parseNodeTypeName = EnumUtil.getNames(ParseNodeTypeEnum.class);
        for (String name : parseNodeTypeName) {
            counters.put(name, 1);
        }
    }

    public Map<String, Integer> counterMap() {
        return this.counters;
    }

    public void add(String key) {
        if (counters.containsKey(key)) {
            counters.put(key, counters.get(key) + 1);
        } else {
            counters.put(key, 1);
        }
    }

    public int get(String key) {
        if (!counters.containsKey(key)) {
            counters.put(key, 1);
            return 1;
        } else {
            return counters.get(key);
        }
    }

    public void clear() {
        this.counters.clear();
    }
}
